﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 客户物资编码适配系统
{
    public partial class frmMain : Form
    {
        //用户信息
        UserInfo ui;

        //配置文件路径及文件名
        string strConfigFile =Application .StartupPath + "\\config.ini";

        //检查时间间隔，单位：秒
        int iCheckTime = 30;

        //异常结果显示窗体
        frmDetail newFrmDetail;

        SqlTools sqt;

        public frmMain(UserInfo tmpui)
        {
            InitializeComponent();
            ui = tmpui;
            timer1.Interval = iCheckTime * 1000;
            sqt = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
            ReadSavedCustomers();
            iniComboBox();
            newFrmDetail = new frmDetail();
        }

        private void ReadSavedCustomers()
        {
            lstCustomers.Items.Clear();
            string strSettings = string.Empty;
            strSettings=INIOperationClass.INIGetStringValue(strConfigFile, "Settings", "CustomerIDs", string.Empty);
            if (!strSettings.Equals (string.Empty ))
            {
                foreach (string strTmp in strSettings.Split (new char[] { ';' }))
                {
                    if (strTmp .Trim ().Equals (string.Empty ))
                    {
                        continue;
                    }
                    lstCustomers.Items.Add(strTmp.Trim());
                }
            }
        }

        private void SaveCustomers()
        {
            string strTmp = string.Empty;
            if (lstCustomers .Items.Count >0)
            {
                for(int i=0;i<lstCustomers .Items.Count;i++)
                {
                    strTmp += GeneralTools.GetString(lstCustomers.Items[i], string.Empty);
                    if(i<lstCustomers .Items.Count -1)
                    {
                        strTmp += ";";
                    }
                }
            }
            INIOperationClass .INIWriteValue(strConfigFile , "Settings", "CustomerIDs", strTmp );
        }

        private void iniComboBox()
        {
            cmbCustomerName.Items.Clear();
            sqt.FillComboBox(cmbCustomerName, "select distinct CustomerName 客户名称 from CustomerMaterialAdapt order by CustomerName");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cmbCustomerName .Text .Trim ().Equals (string.Empty ))
            {
                GeneralTools.ShowInfoMsg("客户名称不能为空！");
                cmbCustomerName.Focus();
                return;
            }
            foreach(object tmpobj in lstCustomers .Items )
            {
                if (tmpobj .Equals (cmbCustomerName.Text .Trim ()))
                {
                    GeneralTools.ShowInfoMsg("客户名称" + cmbCustomerName.Text.Trim() + "已存在！");
                    return;
                }
            }
            lstCustomers.Items.Add(cmbCustomerName.Text.Trim());
            SaveCustomers();
        }

        private void lstCustomers_DoubleClick(object sender, EventArgs e)
        {
            if (lstCustomers .SelectedIndex >=0)
            {
                lstCustomers.Items.RemoveAt(lstCustomers.SelectedIndex);
                SaveCustomers();
            }
        }

        private void btnDataMainten_Click(object sender, EventArgs e)
        {
            frmDataMainten newFrmDataMainten = new frmDataMainten(ui);
            newFrmDataMainten.ShowDialog();
            iniComboBox();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (lstCustomers.Items.Count > 0)
            {
                cmbCustomerName.Enabled = false;
                lstCustomers.Enabled = false;
                btnAdd.Enabled = false;
                btnStart.Enabled = false;
                btnDataMainten.Enabled = false;
                btnStop.Enabled = true;
                timer1.Enabled = true;
                this.Hide();
                notifyIcon1.Visible = true;
            }
            else
            {
                lstCustomers.Focus();
            }
        }

        private void CheckData()
        {
            //默认脚本，仅检查当月合同
            string strHtSelectString = "select hta004 客户名称,hta011 合同号,htb001 订单编号,htb002 订单项次,htb003 型号规格,htb027 物资代码 from hta_table ,htb_table where hta001=htb001 and hta014='Y' and hta021='正常订单' and left(hta002,6)=left(convert(varchar(10),getdate(),112),6) and (";
            string strAdSelectString = "select CustomerName 客户名称,MaterialID 物资代码,OwnModel 型号规格 from CustomerMaterialAdapt where Ignore =0 and (";
            //拼接客户编码脚本
            for (int i = 0; i < lstCustomers.Items.Count; i++)
            {
                strHtSelectString += " hta004 ='" + lstCustomers.Items[i].ToString() + "'";
                strAdSelectString += " CustomerName = '" + lstCustomers.Items[i].ToString() + "'";
                if (i < lstCustomers.Items.Count - 1)
                {
                    strHtSelectString += " or ";
                    strAdSelectString += " or ";
                }
            }
            strHtSelectString += ")";
            strAdSelectString += ")";
            //拼接查询脚本
            string strSelectString = "select a.客户名称,a.合同号,a.订单编号,a.订单项次,a.物资代码,a.型号规格 订单型号规格,b.型号规格 登记型号规格 from (" + strHtSelectString + ") a,(" + strAdSelectString + ") b where a.客户名称=b.客户名称 and a.物资代码=b.物资代码 and a.型号规格<>b.型号规格 order by a.订单编号,a.订单项次";
            //如果有查询结果，弹出提示窗体
            DataTable tmpdt = sqt.GetData(strSelectString);
            if (tmpdt.Rows.Count >0)
            {
                newFrmDetail.ShowData(tmpdt);
                newFrmDetail.Show();
            }
            else
            {
                newFrmDetail.Hide();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            cmbCustomerName.Enabled = true ;
            lstCustomers.Enabled = true;
            btnAdd.Enabled = true;
            btnStart.Enabled = true;
            btnDataMainten.Enabled = true;
            btnStop.Enabled = false;
            timer1.Enabled = false;
            this.Show ();
            notifyIcon1.Visible = false ;
        }
        
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            CheckData();
        }
    }
}
